#include "strategies/PointIncProbDrawingStrategy.h"

bool PointIncProbDrawingStrategy::pickFromDiscard (CCardStack* hand, const GameState* gameState) {
    bool decidePick;
    // Consider picking from discard; the value increase can be obtained deterministically
    const CCard& pileCard = gameState->getCardsInDiscard().back();
    int discardValue = calculateGain(hand, CCard(pileCard.GetIndex()), true, gameState);
    if (discardValue > 0) {
        // Consider the increase probability by drawing
        int increaseProbability = 0;
        CCardStack possibleCards = gameState->getUnknownCards(gameState->getCurrPlayer());
        if (possibleCards.Size() > 0) {
            int increasingCards = 0;
            for (CONST_VI ci = possibleCards.begin(); ci != possibleCards.end(); ci++) {
                if (calculateGain(hand, CCard(ci->GetIndex()), false, gameState) > 0) {
                    increasingCards++;
                }
            }
            increaseProbability = (int) (100 * ((float) increasingCards / possibleCards.Size()));
        }
        decidePick = (increaseProbability > probabilityThreshold);
        if (decidePick) {
            decisionCertainty = 100 - increaseProbability;
        }
        else {
            decisionCertainty = increaseProbability;
        }
    }
    else {
        decidePick = false;
        decisionCertainty = 100;
    }
    return decidePick;
}

int PointIncProbDrawingStrategy::calculateGain(CCardStack* hand, const CCard& card, bool picked, const GameState* gameState) {
    int ret = 0;
    if (discardStrategy != NULL) {
        hand->Push(card);
        int discardDecision = discardStrategy->pickDiscard(hand, picked, gameState);
        hand->Pop();
        ret = Query::gainFromCard(*hand, card, discardDecision);
    }
    else {
        Query::debugOut(string("Discard strategy not set for PIPDS"), 1);
    }
    return ret;
}
